#include "volatile_queue.h"



void volatile_inqueue(void *arg,uint8_t *pack,uint32_t len)
{
	volatile_queue_t *queue = (volatile_queue_t*)arg;
	
	if( len > MAX_QUEUE_PACK_SIZE || len == 0)
		return;

	memcpy( queue->queue[queue->tail],pack,len);
	queue->pack_size[queue->tail] = len;
	queue->tail+=1;
	if(queue->tail >= MAX_QUEUE_LENGTH)
	{
		queue->tail = 0;
	}
	queue->empty_flag = 0;
	if(queue->tail == queue->head)
	{
		queue->head += 1;
		if(queue->head >= MAX_QUEUE_LENGTH)
		{
			queue->head = 0;
		}
        queue->full_flag = 1;
	}
}


uint32_t volatile_dequeue(void *arg,uint8_t *pack)
{
	volatile_queue_t *queue = (volatile_queue_t*)arg;
	
	if( queue->head == queue->tail )
	{
        queue->empty_flag = 1;
			return 0;	
	}
	
	queue->full_flag = 0;
	memcpy( pack,queue->queue[queue->head],queue->pack_size[queue->head]);
	uint32_t ret = queue->pack_size[queue->head];
	queue->head += 1;

	if(queue->head >= MAX_QUEUE_LENGTH)
	{
		queue->head = 0;
	}
	return ret;
}

uint32_t volatile_get_next_element_size(void *arg)
{
    volatile_queue_t *queue = (volatile_queue_t*)arg;
	if( queue->head == queue->tail )
    {
        queue->empty_flag = 1;
		return 0;	
    }

    return queue->pack_size[queue->head];
}

uint8_t is_volatile_queue_empty(void *arg)
{
    volatile_queue_t *queue = (volatile_queue_t*)arg;

    return queue->empty_flag;
}


